class SessionsController < ApplicationController
  def new
   redirect_to user_bills_page_path if login?
  end

  def create
    Rails.logger.debug "session params = #{params}"
    login_info = params[:session]
    @session = User.find_by_email(login_info[:email])
    Rails.logger.debug "params[:email] = #{login_info[:email]}"
    if @session
      if auth_user(@session, login_info[:password])
        login @session
        @tag = true
        flash[:notice] = "Login success"
        # EmailAsynJob.perform_later(@session)
        # BillsMail.welcome_email(@session).deliver_later
      else
        @tag = false
        flash[:notice] = "Login failed"
        # render :action => "new"
        # return
      end
    else
      flash[:notice] = "acount not exist"
    end
    if @tag
      if Group.all.count == 0
        redirect_to new_group_path
        return
      end
      if !current_user.group
        redirect_to groups_path
      else
        redirect_to user_bills_page_path
      end
    else
      redirect_to user_auth_path
    end
  end

  def destroy
    logout
    flash[:notice] = "Please login your account"
    redirect_to login_path
  end
end
